<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Glperiods_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    public function search_gperiods($fiscal,$period,$status){
      $novalue = 1;
      $sql;
        if($period != "" && $fiscal != "" && $status != ""){
          $sql = "SELECT * FROM gl_periods WHERE fiscal_year LIKE ".$this->db->escape($fiscal)." AND period_name LIKE '".$period."' AND status   LIKE '".$status."' ";
        }else if($period == "" && $fiscal != "" && $status != ""){
          $sql = "SELECT * FROM gl_periods WHERE fiscal_year LIKE ".$this->db->escape($fiscal)." AND status LIKE '".$status."' ";
        }else if($period != "" && $fiscal != "" && $status == ""){
          $sql = "SELECT * FROM gl_periods WHERE period_name LIKE ".$this->db->escape($period)." AND fiscal_year LIKE '".$fiscal."' ";
        }else if($period != "" && $fiscal == "" && $status != ""){
          $sql = "SELECT * FROM gl_periods WHERE period_name LIKE ".$this->db->escape($period)." AND period_name LIKE '".$period."' ";
        }else if($period != "" && $fiscal == "" && $status == ""){
          $sql = "SELECT * FROM gl_periods WHERE period_name LIKE ".$this->db->escape($period)."";
        }else if($period == "" && $fiscal != "" && $status == ""){
          $sql = "SELECT * FROM gl_periods WHERE fiscal_year LIKE ".$this->db->escape($fiscal)."";
        }else if($period == "" && $fiscal == "" && $status != ""){
          $sql = "SELECT * FROM gl_periods WHERE status LIKE ".$this->db->escape($status)."";
        }else{
          $novalue = 0;
        }
      if($novalue = 1){
        $query = $this->db->query($sql);
        if($query->num_rows() != 0){
        return $query->result();
        }
        else {
          $result = "No results found.";
          return $result;
        }
      }else{
      $result = "No results found.";
          return $result;
           $novalue = 1;
      }
    }

    public function insertGP($data){
      $p = $this->db->query('call gl_periods_dml(?,?,?,?,?,?,?,?,?,@pid,@err,@msg)',$data);
      $que = $this->db->query("SELECT @pid as lid, @err as err,@msg as msg");
      $msg; $err; $lid;
      foreach($que->result() as $keys){
        $err = $keys->err; $msg = $keys->msg;$lid = $keys->lid;
      }
      echo json_encode(array('err'=>$err,'msg'=>$msg,'lid'=>$lid));
    }

    public function updateGP($data){
      $p = $this->db->query('call gl_periods_dml(?,?,?,?,?,?,?,?,?,@pid,@err,@msg)',$data);
      $que = $this->db->query("SELECT @pid as lid, @err as err,@msg as msg");
      $msg; $err; $lid;
      foreach($que->result() as $keys){
        $err = $keys->err; $msg = $keys->msg;$lid = $keys->lid;
      }
      echo json_encode(array('err'=>$err,'msg'=>$msg,'lid'=>$lid));
    }

    public function getgp($id){
      $this->db->where('period_id',$id);
      $query = $this->db->get('gl_periods_v');
      return $query->result();
    }

    public function getgps(){
      $query = $this->db->get('gl_period_status_v');
      return $query->result();
    }
}